package com.tepth.vehiclemaintenance.mvp.work.content;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tepth.vehiclemaintenance.R;
import com.tepth.vehiclemaintenance.adapter.GuojianAdapter;
import com.tepth.vehiclemaintenance.bean.MaintainListWork;
import com.tepth.vehiclemaintenance.event.GuoJianPunchCardEvent;
import com.tepth.vehiclemaintenance.event.GuoJianSelectEvent;
import com.tepth.vehiclemaintenance.tools.DimensionConvert;
import com.tepth.vehiclemaintenance.tools.Utils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by LHQ on 2016/12/19.
 */

public class GuojianBottomFragment extends DialogFragment {

    private ArrayList<MaintainListWork> works;

    @BindView(R.id.lv_guojian_list)
    ListView lv_guojian_list;

    @BindView(R.id.rl_guojian_bottom)
    RelativeLayout bottom_button;
    private VectorDrawableCompat mDrawableCompatNormal;
    private VectorDrawableCompat mDrawableCompatCancel;
    private VectorDrawableCompat mDrawableCompatComplete;

    private int mDosize;
    private int mCancelSize;

    public static GuojianBottomFragment newInstance(ArrayList<MaintainListWork> workList,
                                                    int doSize, int cancelSize) {
        GuojianBottomFragment instance = new GuojianBottomFragment();
        Bundle args = new Bundle();
        args.putSerializable("list", workList);
        args.putInt("doSize", doSize);
        args.putInt("cancelSize", cancelSize);
        instance.setArguments(args);
        return instance;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        works = (ArrayList<MaintainListWork>) getArguments().getSerializable("list");
        mDosize = getArguments().getInt("doSize");
        mCancelSize = getArguments().getInt("cancelSize");

        // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
        Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        dialog.setContentView(R.layout.fragment_guojian_bottom);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消

        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.height = DimensionConvert.getHeigh2(getActivity());//高度为一半
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        window.setAttributes(lp);

        ButterKnife.bind(this, dialog); // Dialog即View

        initView();
        initListener();
        return dialog;
    }

    private void initView() {
        GuojianAdapter adapter = new GuojianAdapter(getActivity(), works);
        lv_guojian_list.setAdapter(adapter);
        if (!(mDosize == 0 && mCancelSize == 0)) {
            bottom_button.setBackgroundColor(Utils.getColor(getActivity(), R.color.green_scan_to_barcode_n));
        } else {
            bottom_button.setBackgroundColor(Utils.getColor(getActivity(), R.color.grey_scan_to_barcode));
        }
        mDrawableCompatNormal = VectorDrawableCompat.create(getContext().getResources(),
                R.drawable.ic_check_circle_gray_24dp, getContext().getTheme());
        mDrawableCompatCancel = VectorDrawableCompat.create(getContext().getResources(),
                R.drawable.ic_check_circle_red_24dp, getContext().getTheme());
        mDrawableCompatComplete = VectorDrawableCompat.create(getContext().getResources(),
                R.drawable.ic_check_circle_green_24dp, getContext().getTheme());
    }

    private void guojian_list_Listener(MaintainListWork obj, int type) {
        switch (type) {
            case 0:
                mDosize += 1;
                break;
            case 1:
                mDosize -= 1;
                break;
            case 2:
                mCancelSize += 1;
                break;
            case 3:
                mCancelSize -= 1;
                break;
        }
    }

    private void initListener() {
        lv_guojian_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                MaintainListWork obj = works.get(i);
                ImageView checkbox = (ImageView) view.findViewById(R.id.guojian_checkbox);
                TextView tv_guojian_details = (TextView) view.findViewById(R.id.tv_guojian_details);
                TextView tv_guojian_name = (TextView) view.findViewById(R.id.tv_guojian_name);
                ImageView ivRemark = (ImageView) view.findViewById(R.id.iv_remark);
                if (obj.getConfirmPersonName() == null || obj.getConfirmPersonName().isEmpty() ||
                        "null".equals(obj.getConfirmPersonName())) {
                    tv_guojian_name.setVisibility(View.GONE);
                    tv_guojian_details.setTextColor(Utils.getColor(getActivity(), R.color.colorItemTextBlack));
                    if (!obj.isGuojian()) {
                        guojian_list_Listener(obj, 0);
                        EventBus.getDefault().post(new GuoJianSelectEvent(obj, 0));
                        checkbox.setImageDrawable(mDrawableCompatCancel);
                        obj.setGuojian(true);
                        ivRemark.setVisibility(View.GONE);
                    } else {
                        guojian_list_Listener(obj, 1);
                        EventBus.getDefault().post(new GuoJianSelectEvent(obj, 1));
                        checkbox.setImageDrawable(mDrawableCompatNormal);
                        obj.setGuojian(false);
                        ivRemark.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (tv_guojian_details.getCurrentTextColor() == Utils.getColor(getActivity(), R.color.colorPrimary)) {
                        tv_guojian_name.setVisibility(View.VISIBLE);
                        tv_guojian_name.setText(obj.getConfirmPersonName());
                        guojian_list_Listener(obj, 3);
                        EventBus.getDefault().post(new GuoJianSelectEvent(obj, 3));
                        tv_guojian_details.setTextColor(Utils.getColor(getActivity(), R.color.colorItemTextBlack));
                        checkbox.setImageDrawable(mDrawableCompatComplete);
                        ivRemark.setVisibility(View.GONE);
                    } else {
                        tv_guojian_name.setVisibility(View.GONE);
                        guojian_list_Listener(obj, 2);
                        EventBus.getDefault().post(new GuoJianSelectEvent(obj, 2));
                        tv_guojian_details.setTextColor(Utils.getColor(getActivity(), R.color.colorPrimary));
                        checkbox.setImageDrawable(mDrawableCompatCancel);
                        ivRemark.setVisibility(View.VISIBLE);
                    }
                    obj.setGuojian(false);
                }
                if (!(mDosize == 0 && mCancelSize == 0)) {
                    bottom_button.setBackgroundColor(Utils.getColor(getActivity(), R.color.green_scan_to_barcode_n));
                } else {
                    bottom_button.setBackgroundColor(Utils.getColor(getActivity(), R.color.grey_scan_to_barcode));
                }
            }
        });
        bottom_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new GuoJianPunchCardEvent());
            }
        });
    }

}
